package com.wootion.cims.module.systemsetting.log.service.impl;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wootion.cims.dao.SysLogDao;
import com.wootion.cims.dao.UserDao;
import com.wootion.cims.module.systemsetting.log.model.SysLog;
import com.wootion.cims.module.systemsetting.log.service.SysLogService;
import com.wootion.cims.response.ResponseResult;
import com.wootion.cims.response.ResultCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.Optional;

@Service
public class SysLogServiceImpl extends ServiceImpl<SysLogDao, SysLog> implements SysLogService {

    @Autowired(required = false)
    SysLogDao sysLogDao;

    @Autowired(required = false)
    UserDao userDao;
    @Override
    public ResponseResult selectPage(Integer limit, Integer page, String condition, Date begintime, Date endtime, String value, String inciden, String type, String result) {
        Page<SysLog> page1 = new Page<>(page,limit);

        QueryWrapper<SysLog> queryWrapper = new QueryWrapper<>();
//        queryWrapper.eq("enabled",true);
        queryWrapper.eq(StringUtils.hasText(condition),"operator",condition);
        if (Optional.ofNullable(begintime).isPresent() && Optional.ofNullable(endtime).isPresent()){
            queryWrapper.between("create_time",begintime,endtime);
        }

        if (Optional.ofNullable(value).isPresent()&& !StringUtils.isEmpty(value)){
            queryWrapper.eq("value",value);
        }
        if (Optional.ofNullable(inciden).isPresent()&& !StringUtils.isEmpty(inciden)){
            queryWrapper.eq("incident",inciden);
        }
        if (Optional.ofNullable(type).isPresent()&& !StringUtils.isEmpty(type)){
            queryWrapper.eq("type",type);
        }
        if (Optional.ofNullable(result).isPresent() && !StringUtils.isEmpty(result)){
            queryWrapper.eq("result",result);
        }
        queryWrapper.orderByDesc("create_time");
        Page<SysLog> userPage = sysLogDao.selectPage(page1, queryWrapper);
        return ResponseResult.builder().code(ResultCode.NORMAL.getCode()).message(ResultCode.NORMAL.getMessage()).data(userPage).build();
    }
}
